<?php
class AdsCampaignsController extends LiladsAppController {
	var $name = 'AdsCampaigns';
	var $uses = array('LilAds.AdsCampaign');
/**
 * isAuthorized method
 *
 * @access public
 * @return bool
 */
	function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		
		// plugins can also change authorization
		$c = $this->callPluginHandlers('isAuthorizedLilAds', array('return' => true, 'continue' => true));
		if (!isset($c['return']) || $c['return'] == false) return false;
		
		// plugin can determine if it wants to end authorization check
		if (!isset($c['continue']) || $c['continue'] == true) {
			return $this->isAdmin();
		}
		return true;
	}
	
	function admin_stats() {
		$this->set('stats', true);
		$this->setAction('admin_index');
	}

	function admin_index() {
		$params = array_merge($this->params['url'], $this->params['named']);
		if (isset($params['url'])) unset($params['url']);
		
		$this->set('campaigns', $this->paginate($this->AdsCampaign->conditions($params)));
		$this->set('params', $params);
	}
	
	function admin_add() {
		$this->setAction('admin_edit');
	}

	function admin_edit($id = null) {
		if (!empty($this->data)) {
			if ($this->AdsCampaign->save($this->data)) {
				$this->Session->setFlash(__d('lil_ads', 'Campaign has been saved.', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__d('lil_ads', 'Please verify that the information is correct.', true), 'error');
			}
		}
		if (is_numeric($id)) {
			$this->data = $this->AdsCampaign->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (is_numeric($id) && $this->AdsCampaign->delete($id)) {
			$this->Session->setFlash(sprintf(__d('lil_ads', 'Campaign #%s has been deleted.', true), $id));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->error404();
		}
	}

	function admin_activate($id = null) {
		if (is_numeric($id) && $this->AdsCampaign->activate($id)) {
			$this->Session->setFlash(sprintf(__d('lil_ads', 'Campaign #%s has been activated.', true), $id));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->error404();
		}
	}

	function admin_refresh($id = null) {
		if (is_numeric($id) && $this->AdsCampaign->refresh($id)) {
			$this->Session->setFlash(sprintf(__d('lil_ads', 'Campaign #%s stats have been updated.', true), $id));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->error404();
		}
		
	}
}
?>